package com.union.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.net.Uri;
import android.os.IBinder;
import android.text.Selection;
import android.text.Spannable;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SystemUtils
{
	private SystemUtils()
	{
	}
	
	/**
	 * 获取Session
	 * 
	 * @return
	 */
	public static String getSession()
	{
		return PreferUtils.getString("session", "");
	}
	
	/**
	 * 是否是平?
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isTablet(Context context)
	{
		return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
	}
	
	public static void toggleSoftKeyboard(Context context)
	{
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
	}
	
	public static void showSoftKeyboard(Context context, View view)
	{
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
	}
	
	public static void hideSoftKeyboard(Context context, View view)
	{
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}
	
	/**
	 * 安装APK
	 * 
	 * @param context
	 */
	public static void installApk(Context context)
	{
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(new File(context.getCacheDir(), "XXXXX.apk")),
				"application/vnd.android.package-archive");
		context.startActivity(intent);
	}
	
	/**
	 * 更改APK权限
	 * 
	 * @param context
	 */
	public static void chmodApk(Context context)
	{
		try
		{
			Runtime.getRuntime().exec("chmod 777 " + new File(context.getCacheDir(), "XXXXX.apk").toString());
		} catch (IOException e)
		{
			Log.e("", e.getMessage());
		}
	}
	/**
	 * 获取版本号?
	 * 
	 * @param context
	 * @return
	 */
	public static int getVersionCode(Context context) throws NameNotFoundException
	{
		return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
	}
	
	/**
	 * 获取版本名?
	 * 
	 * @param context
	 * @return
	 */
	public static String getVersionName(Context context) throws NameNotFoundException
	{
		return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
	}
	
	
	/**
	 * 打卡软键?
	 */
	public static void openKeybord(EditText mEditText, Context mContext)
	{
		InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
	}
	
	/**
	 * 关闭软键盘?
	 */
	public static void closeKeybord(IBinder windowToken, Context mContext)
	{
		InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(windowToken, 0);
	}
	//选中EditText文本内容
	public static void selectEditContent(EditText et)
	{
		CharSequence text = et.getText();
		if (text instanceof Spannable)
		{
			Selection.setSelection((Spannable) text, 0, text.length());
		}
	}
	/** 
	 * 查询手机内非系统应用 
	 * @param context 
	 * @return 
	 */ 
	public static List<PackageInfo> getAllApps(Context context) {
		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		PackageManager pManager = context.getPackageManager();
		List<PackageInfo> paklist = pManager.getInstalledPackages(0);
		for (int i = 0; i < paklist.size(); i++) { 
			PackageInfo pak = (PackageInfo) paklist.get(i);
			if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
				// customs applications 
				apps.add(pak); 
			} 
			Log.i("grage", pak.applicationInfo.packageName);//包名
			Log.i("grage", pManager.getApplicationLabel(pak.applicationInfo).toString());
		}
//		List<PackageInfo> appList = Utils.getAllApps(context);
//		
//		for(int i=0;i<appList.size();i++) { 
//			PackageInfo pinfo = appList.get(i); 
//			ShareItemInfo shareItem = new ShareItemInfo(); 
//			//set Icon 
//			shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo)); 
//			//set Application Name 
//			shareItem.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString()); 
//			//set Package Name 
//			shareItem.setPackageName(pinfo.applicationInfo.packageName); 
//			
//		}
		return apps; 
	}
	/**
	 * @param root 最外层布局，需要调整的布局
	 * @param scrollToView 被键盘遮挡的scrollToView，滚动root,使scrollToView在root可视区域的底部
	 */
	public static  void controlKeyboardLayout(final View root, final View scrollToView)
	{
		root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
		{
			@Override
			public void onGlobalLayout()
			{
				Rect rect = new Rect();
				// 获取root在窗体的可视区域
				root.getWindowVisibleDisplayFrame(rect);
				// 获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
				int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
				// 若不可视区域高度大于100，则键盘显示
				if (rootInvisibleHeight > 100)
				{
					int[] location = new int[2];
					// 获取scrollToView在窗体的坐标
					scrollToView.getLocationInWindow(location);
					// 计算root滚动高度，使scrollToView在可见区域
					int srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
					root.scrollTo(0, srollHeight);
				} else
				{
					// 键盘隐藏
					root.scrollTo(0, 0);
				}
			}
		});
	}
}

	
